package org.apache.struts2.plugin.json.reflection;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.struts2.plugin.json.model.JSONString;

/**
 * @author VERDOÏA Laurent <verdoialaurent@gmail.com>
 */
public abstract class JSONStringReflection {

    public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

    public static JSONString reflection(Object o) {
        if (o instanceof Class) {
            return fromClass((Class) o);
        } else if (o instanceof String) {
            return fromString((String) o);
        } else if (o instanceof Character) {
            return fromCharacter((Character) o);
        } else if (o instanceof Date) {
            return fromDate((Date) o);
        } else if (o instanceof Calendar) {
            return fromCalendar((Calendar) o);
        } else if (o instanceof Enum) {
            return fromEnum((Enum) o);
        }
        return null;
    }

    public static JSONString fromClass(Class o) {
        return new JSONString(o.toString());
    }

    public static JSONString fromString(String o) {
        return new JSONString(o);
    }

    public static JSONString fromCharacter(Character o) {
        return new JSONString(o.toString());
    }

    public static JSONString fromDate(Date o) {
        return fromDate(o, DEFAULT_DATE_FORMAT);
    }

    public static JSONString fromDate(Date o, String format) {
        return new JSONString(new SimpleDateFormat(format).format(o));
    }

    public static JSONString fromCalendar(Calendar o) {
        return fromCalendar(o, DEFAULT_DATE_FORMAT);
    }

    public static JSONString fromCalendar(Calendar o, String format) {
        return new JSONString(new SimpleDateFormat(format).format(o.getTime()));
    }

    public static JSONString fromEnum(Enum o) {
        return new JSONString(o.name());
    }
}
